﻿#if UNITY_EDITOR
using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using SimpleJSON;
using System.Runtime.Serialization.Formatters.Binary;
using System;
using UnityEditor.SceneManagement;

 
public class ExporterSKFB : EditorWindow
{
 
	[MenuItem("GameObject/Export SKFB GLTF #UP", false, 11)] //11及以后可以在Camera之后显示
	static void Init ()
	{
#if UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX // edit: added Platform Dependent Compilation - win or osx standalone
	ExporterSKFB window = (ExporterSKFB)EditorWindow.GetWindow (typeof(ExporterSKFB));
	window.titleContent.text = "Unity2Skfb";
	window.Show ();
#else // and error dialog if not standalone
	EditorUtility.DisplayDialog("Error", "Your build target must be set to standalone", "Okay");
#endif
	}

	// UI dimensions (to be cleaned)
	[SerializeField]
	Vector2 loginSize = new Vector2 (603, 190);
	[SerializeField]
	Vector2 fullSize = new Vector2 (603, 290);
	[SerializeField]
	Vector2 descSize = new Vector2 (603, 175);

	// Fields limits
	const int NAME_LIMIT = 48;
	//	const int TAGS_LIMIT = 50;
	const int PASSWORD_LIMIT = 64;
	const int SPACE_SIZE = 5;

	// Keys used to save credentials in editor prefs
	const string usernameEditorKey = "UnityExporter_username";

	// Exporter UI: static elements
	[SerializeField]
	private string clickableLabelColor = "navy";
	string access_token = "";
	GameObject exporterGo;
	SceneToGlTFWiz exporter;
	private string exportPath;
	private string zipPath;

	//Fields
	private string param_name = "";
	private bool opt_exportAnimation = true;

	// Exporter UI: dynamic elements
	private string status = "";
	private Color blueColor = new Color (69 / 255.0f, 185 / 255.0f, 223 / 255.0f);
	private Color redColor = new Color (0.8f, 0.0f, 0.0f);
	private Color greyColor = Color.white;

	//private List<String> tagList;
	void Awake ()
	{
		exporterGo = new GameObject ("Exporter");
		exporter = exporterGo.AddComponent<SceneToGlTFWiz> ();
	}

	void OnSelectionChange ()
	{
		// do nothing for now
//		string name = Selection.activeGameObject.name;
//		if(param_name=="")
//			 
	}

	 
	private bool updateExporterStatus ()
	{
		status = "";
		int nbSelectedObjects = Selection.GetTransforms (SelectionMode.Deep).Length;
		if (nbSelectedObjects == 0) {
			status = "No object selected to export";
			return false;
		}
		status = "Export " + nbSelectedObjects + " object: "+ param_name + " " ;
		return true;
	}

	void OnGUI ()
	{

		// Model name
		GUILayout.Label("Name");
		param_name = EditorGUILayout.TextField(param_name);
		GUILayout.Label("(" + param_name.Length + "/" + NAME_LIMIT + ")", EditorStyles.centeredGreyMiniLabel);
		EditorStyles.textField.wordWrap = true;
		GUILayout.Space(SPACE_SIZE);

		GUILayout.Label ("Options", EditorStyles.boldLabel);
		GUILayout.BeginHorizontal ();
		opt_exportAnimation = EditorGUILayout.Toggle ("Export animation (beta)", opt_exportAnimation);
		GUILayout.FlexibleSpace ();
		GUILayout.EndHorizontal ();
		bool enable = updateExporterStatus ();

		if (enable)
			GUI.color = blueColor;
		else
			GUI.color = greyColor;

		GUI.enabled = enable;
		GUILayout.BeginHorizontal ();
		GUILayout.FlexibleSpace ();
		if (GUILayout.Button (status, GUILayout.Width (250), GUILayout.Height (40))) {
			if (!enable) {
				EditorUtility.DisplayDialog ("Error", status, "Ok");
			}else if(param_name == "")
            {
                EditorUtility.DisplayDialog("Error", "Please input file name", "Ok");
            }else {
				exportPath = Application.dataPath + "/" + param_name +".gltf";
				if (File.Exists (exportPath)) {
					EditorUtility.DisplayDialog ("Exists", "File Exists,Please rename and try again.", "Ok");
				}else{
					exporter.ExportCoroutine (exportPath, null, false, true, opt_exportAnimation, true);
					if (File.Exists (exportPath)) {
						EditorUtility.DisplayDialog ("OK", exportPath, "Ok");
					} 
				}

			}
		}
		GUILayout.FlexibleSpace ();
		GUILayout.EndHorizontal ();
	}

	void OnDestroy ()
	{
		if (System.IO.File.Exists (zipPath))
			System.IO.File.Delete (zipPath);

		if (exporterGo) {
			DestroyImmediate (exporterGo);
			exporter = null;
		}
	}
}

#endif